package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.investigate.BpInvestigateStoreDetailBean;
import com.joysuch.wwyt.bp.entity.investigate.BpInvestigateStoreDetail;
import com.joysuch.wwyt.bp.service.BpInvestigateStoreDetailService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.dataimport.service.DataImportService;
import com.joysuch.wwyt.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/3/25 14:29
 */
@RestController
@RequestMapping("/bp/investigate/store/detail")
@Api(tags = "隐患管理-检查标准库-排查内容")
@Slf4j
public class BpInvestigateStoreDetailController {

    @Autowired
    private BpInvestigateStoreDetailService bpInvestigateStoreDetailService;
    @Autowired
    private DataImportService dataImportService;

    @PostMapping("save")
    @RequiresPermissions("bp:investigate:store:detail:save")
    @ApiOperation("新增/修改排查内容")
    public ResultBean save(@RequestBody @Valid BpInvestigateStoreDetailBean bean) {
        return bpInvestigateStoreDetailService.save(bean);
    }

    @GetMapping("/delete/{id}")
    @RequiresPermissions("bp:investigate:store:detail:delete")
    @ApiOperation(value = "根据标准库Id删除")
    @ApiImplicitParam(name = "id", value = "标准库Id", paramType = "path", required = true)
    public ResultBean delete(@PathVariable Long id) {
        return bpInvestigateStoreDetailService.delete(id);
    }

    @PostMapping("/deletes")
    @RequiresPermissions("bp:investigate:store:detail:delete")
    public ResultBean delete(@NotEmpty(message = "请勾选删除项") @RequestBody List<Long> ids) {
        return bpInvestigateStoreDetailService.deletes(ids);
    }

    @GetMapping("/page/{storeId}")
    @ApiOperation(value = "检查标准库列表", response = BpInvestigateStoreDetail.class)
    @ApiImplicitParam(name = "storeId", value = "标准库Id", paramType = "path", required = true)
    public ResultBean list(Pageable pageable, @PathVariable Long storeId) {
        return bpInvestigateStoreDetailService.page(pageable, storeId);
    }

    @GetMapping("download/template")
    @ApiOperation(value = "下载导入模板")
    public void download(HttpServletResponse response, HttpServletRequest request) {
        String fileName = "隐患标准库导入模板.xlsx";
        ClassPathResource classPathResource = new ClassPathResource("template" + File.separator + fileName);
        try (ServletOutputStream outputStream = response.getOutputStream();
             InputStream inputStream = classPathResource.getInputStream()) {
            fileName = FileUtil.getEncodeFileName(request, fileName);
            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            byte[] data = new byte[1024 * 10];
            int len = -1;
            while ((len = inputStream.read(data)) != -1) {
                outputStream.write(data, 0, len);
            }
        } catch (Exception e) {
            log.error("隐患标准库导入模板.xlsx 下载失败", e);
        }
    }

    @PostMapping("import/data")
    @ApiOperation("导入数据")
    @RequiresPermissions("bp:investigate:store:detail:import:data")
    public ResultBean importData(MultipartFile file) {
        return dataImportService.importInvestigateData(file);
    }

}
